/*
 * Copyright (C), 2015-2019
 * FileName: MemberInfo
 * Author:   lizhao
 * Date:     2019-02-11 18:59
 * Description: 字段或者方法对应的数据结构
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaoblog.jjvm.classfile;

import com.lizhaoblog.jjvm.classfile.attribute.AttributeInfo;
import com.lizhaoblog.jjvm.classfile.attribute.BaseAttributeInfo;
import com.lizhaoblog.jjvm.classfile.constantpool.ConstantPool;

import java.util.Arrays;

/**
 * 〈一句话功能简述〉<br>
 * 〈字段或者方法对应的数据结构〉
 *
 * @author lizhao
 * @date 2019-02-11 18:59
 * @since 1.0.0
 */
public class MemberInfo {
    private ConstantPool constantPool;

    private int accessFlags;
    private int nameIndex;
    private int descriptorIndex;
    private AttributeInfo[] attributes;

    public MemberInfo(ConstantPool constantPool, ClassReader reader) {
        this.constantPool = constantPool;
        accessFlags = reader.nextU2ToInt();
        nameIndex = reader.nextU2ToInt();
        descriptorIndex = reader.nextU2ToInt();
        attributes = BaseAttributeInfo.readAttributes(reader, constantPool);
    }

    public static MemberInfo[] readMembers(ConstantPool constantPool, ClassReader reader) {
        int fieldCount = reader.nextU2ToInt();
        MemberInfo[] fields = new MemberInfo[fieldCount];

        for (int i = 0; i < fieldCount; i++) {
            fields[i] = new MemberInfo(constantPool, reader);
        }

        return fields;
    }

    @Override
    public String toString() {
        return "MemberInfo{" +
                ", accessFlags=" + accessFlags +
                ", nameIndex=" + nameIndex +
                ", descriptorIndex=" + descriptorIndex +
                ", attributes=" + Arrays.toString(attributes) +
                "}\n";
    }
}
